package cn.anecansaitin.whimsy.common.network;

import cn.anecansaitin.whimsy.Whimsy;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Abilities;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;

public record StunEffectPack(boolean hasEffect) implements CustomPacketPayload {
    public static final ResourceLocation ID = Whimsy.resource("stun_effect_pack");
    public static final ClientHandler HANDLER = new ClientHandler();

    public StunEffectPack(FriendlyByteBuf buffer) {
        this(buffer.readBoolean());
    }
    @Override
    public void write(FriendlyByteBuf buffer) {
        buffer.writeBoolean(hasEffect);
    }

    @Override
    public ResourceLocation id() {
        return ID;
    }

    public static class ClientHandler {
        private ClientHandler() {
        }

        public void handle(StunEffectPack data, PlayPayloadContext context) {
            context.workHandler().submitAsync(() -> {
                Abilities abilities = Minecraft.getInstance().player.getAbilities();

                if (data.hasEffect) {
                    abilities.setFlyingSpeed(0);
                } else {
                    abilities.setFlyingSpeed(0.05f);
                }
            });
        }
    }
}
